package com.java.base.exercise.current.alarmsystem;
import com.sun.corba.se.impl.orbutil.fsm.GuardedAction;
import net.sf.cglib.core.Predicate;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author RenPu
 * @Date 2022/1/6 17:39
 * @Version 1.0
 * @Description: 报警系统伪代码编写
 **/
public class AlarmSystem {


    /**
     *  volatile: 线程可见性，指令重排序
     */
    volatile boolean  connectToServer=false;


    /**
     * 利用cglib的对象进行熔断, 保护性条件
     */
    Predicate ageConnected= new Predicate() {
        @Override
        public boolean evaluate(Object arg) {
            return connectToServer;
        }
    };



    //初始化报警服务：1：其他服务于报警服务器连接，2：定时调度线程每5s检查下连接状态
    public void init(){

        //线程异步发送与，报警服务器建立链接
        Thread connectThread = new Thread(new ConnectTask());
        connectThread.start();


        //定时线程池的创建
        ScheduledThreadPoolExecutor heartBeatExecutor = new ScheduledThreadPoolExecutor(5, new ThreadFactory() {

            /**
             * 构建线程池中线程的一些属性
             */
            private AtomicInteger index=new AtomicInteger();
            @Override
            public Thread newThread(Runnable runnable) {
                Thread thread = new Thread();
                thread.setName("heart-beat-thread"+index);
                thread.setDaemon(true);
                return thread;
            }
        });

        //定时5s检查连接状态
        heartBeatExecutor.scheduleAtFixedRate(new HeatTask(),5000,2000, TimeUnit.MICROSECONDS);


    }


    /**
     *
     */
    public void sendAlarm(AlarmInfo alarmInfo){

    }







/**
 *    报警服务器连接对象
  */
class ConnectTask implements Runnable{

        private Blocker blocker;


    @Override
    public void run() {

        //走socketChannel的方式和告警服务器建立连接

        try {
            Thread.sleep(1000); //需要根据告警服务器  ip+port进行进行连接，在这模拟下连接的动作
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("连接建立完成........");
        onConnected();

    }


    private void onConnected(){
        blocker.signalAfter(new Callable<Boolean>(){

            @Override
            public Boolean call() throws Exception {

                //修改唤醒前的动作
                //修改连接报警服务器的状态
                System.out.println("update connectToServer=true");
                connectToServer=true;

                //条件满足执行，唤醒
                return Boolean.TRUE;
            }
        });

    }
}







interface Blocker{
   boolean signalAfter(Callable callable);
}





class HeatTask implements Runnable{

    /**
     * 判断连接是否正常
     * 1：不正常就，
     * 2：断开连接
     * 3：重新进行连接
      */

    @Override
    public void run() {
        if(!testConnect()){
            //断开连接
            onDisconnect();
            //重试建立连接
            reConnect();

        }

    }


    //重试连接
    private void reConnect() {
        ConnectTask connectTask = new ConnectTask();
        connectTask.run();


    }


    //断开连接
    private void onDisconnect() {

    }


    //测试连接
    private boolean testConnect() {

        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test connect normal.....");
        return true;
    }
}




}
